package com.xbenben.project.common.init;

import com.xbenben.project.common.constant.CommonDict;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * @author xbenbenx
 * Date:2022/11/21 23:28
 **/
@Slf4j
@Component
public class LoadInitData implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @PostConstruct
    public void init() {
        long currentTimeMillis = System.currentTimeMillis();
        log.info("【初始化数据同步--开始】【开始时间：{}】", DateFormatUtils.format(new Date(currentTimeMillis), CommonDict.DEFAULT_PATTEN));
        String[] names = applicationContext.getBeanNamesForType(InitData.class);
        if (names.length > 0) {
            for (String name : names) {
                log.info("【初始化数据同步】【开始同步Bean：{}】", name);
                InitData initData = ((InitData) applicationContext.getBean(name));
                initData.loadData();
            }
        }
        log.info("【初始化数据同步--结束】【用时：{}毫秒】", System.currentTimeMillis() - currentTimeMillis);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
